package eu.jba.denshijisho;

import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import eu.jba.denshijisho.adapter.NoteAdapter;
import eu.jba.denshijisho.dao.CardNoteDao;
import eu.jba.denshijisho.dao.NoteDao;
import eu.jba.denshijisho.entity.CardNote;
import eu.jba.denshijisho.exceptionhandler.ExceptionHandler;
import eu.jba.denshijisho.listeners.ListenersHelper;
import eu.jba.denshijisho.util.Constants;

public class ShowNotesActivity extends AbstractDenshiActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Thread.setDefaultUncaughtExceptionHandler( new ExceptionHandler( this ) );
		setContentView(R.layout.activity_show_notes);
		setFinishOnTouchOutside(false);
		
		final ListView lv = (ListView) findViewById(R.id.note_list_view_main);
		lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
		lv.setOnItemClickListener( ListenersHelper.getListViewItemListener() );
		
		final NoteAdapter arrayAdapter = new NoteAdapter(
				this, R.layout.listview_item_row3, getItems());
		lv.setAdapter(arrayAdapter);
		registerForContextMenu(lv);

	}

	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu, v, menuInfo);
		getMenuInflater().inflate(R.menu.context_action_note_menu, menu);
	}
	
	public boolean onContextItemSelected(MenuItem item) {
		final AdapterView.AdapterContextMenuInfo menuInfo = (AdapterView.AdapterContextMenuInfo) item
				.getMenuInfo();
		final ListView lv = (ListView) findViewById(R.id.note_list_view_main);
		final NoteAdapter noteAdapter = (NoteAdapter) lv.getAdapter();
		final CardNote cardNote = noteAdapter.getItem(menuInfo.position);

		if (item.getItemId() == R.id.delete_note) {
			final NoteDao noteDao = new NoteDao( this );
			noteDao.open();
			noteDao.delete( cardNote.getNoteId() );
			noteDao.close();
			final CardNoteDao cardNoteDao = new CardNoteDao( this );
			cardNoteDao.open();
			cardNoteDao.delete( cardNote.getId() );
			cardNoteDao.close();
			updateListView();			
		}
		if(item.getItemId() == R.id.edit_note){
			final Intent edit = new Intent(this, EditNoteActivity.class);
			edit.putExtra(Constants.NOTE_ID, cardNote.getNoteId());
			startActivityForResult(edit, 1);
		}
		
		return super.onContextItemSelected(item);
	}
	
	private void updateListView() {
		final ListView lv = (ListView) findViewById(R.id.note_list_view_main);
		final NoteAdapter arrayAdapter = new NoteAdapter(
				this, R.layout.listview_item_row3, getItems());
		lv.setAdapter(arrayAdapter);
	}
	
	private List<CardNote> getItems(){
		final Bundle bundle = getIntent().getExtras();
		final Integer cardId = (Integer)bundle.getInt(Constants.CARD_ID);
		final CardNoteDao cardNoteDao = new CardNoteDao( this );
		cardNoteDao.open();
		final List<CardNote> items = cardNoteDao.getAll( cardId );
		cardNoteDao.close();
		return items;
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (resultCode == RESULT_OK) {
			updateListView();
		}
	}
}
